package com.jmb.waimao.service;

import com.jmb.waimao.annotation.Tool;
import com.jmb.waimao.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Tool service for the Mail Control Protocol (MCP) Email Service
 * Provides AI-accessible tools for sending emails with extended control features
 */
@Service
public class MCPEmailToolService {

    @Autowired
    private MCPEmailService mcpEmailService;

    /**
     * Send a simple email
     *
     * @param to Recipient email address
     * @param subject Email subject
     * @param content Email content (can be HTML)
     * @return Response with success status and message
     */
    @Tool(description = "Send an email to a recipient with a subject and content. The content can include HTML formatting.")
    public Map<String, Object> sendEmail(
            @ToolParam(description = "Recipient email address") String to,
            @ToolParam(description = "Email subject") String subject,
            @ToolParam(description = "Email content (can include HTML)") String content) {

        boolean success = mcpEmailService.sendEmail(to, subject, content);

        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "Email sent successfully" : "Failed to send email");

        return response;
    }

    /**
     * Send a templated email
     *
     * @param to Recipient email address
     * @param subject Email subject (optional)
     * @param templateId ID of the template to use
     * @param variables Variables to replace in the template
     * @return Response with success status and message
     */
    @Tool(description = "Send a templated email to a recipient. The template is identified by its ID, and variables can be provided to replace placeholders in the template.")
    public Map<String, Object> sendTemplatedEmail(
            @ToolParam(description = "Recipient email address") String to,
            @ToolParam(description = "Email subject (optional)", required = false) String subject,
            @ToolParam(description = "ID of the template to use") Long templateId,
            @ToolParam(description = "Variables to replace in the template") Map<String, Object> variables) {

        boolean success = mcpEmailService.sendTemplatedEmail(to, subject, templateId, variables);

        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "Templated email sent successfully" : "Failed to send templated email");
        response.put("templateId", templateId);

        return response;
    }

    /**
     * Get the status of the email service
     *
     * @return Status information
     */
    @Tool(description = "Get the current status of the email service, including connection status and configuration details.")
    public Map<String, Object> getEmailServiceStatus() {
        return mcpEmailService.getStatus();
    }

    /**
     * Configure the email service
     *
     * @param host SMTP server host
     * @param port SMTP server port
     * @param username SMTP server username
     * @param password SMTP server password
     * @param fromEmail Email address to use as sender
     * @return Response with success status and message
     */
    @Tool(description = "Configure the email service with SMTP server settings.")
    public Map<String, Object> configureEmailService(
            @ToolParam(description = "SMTP server host", required = false) String host,
            @ToolParam(description = "SMTP server port", required = false) Integer port,
            @ToolParam(description = "SMTP server username", required = false) String username,
            @ToolParam(description = "SMTP server password", required = false) String password,
            @ToolParam(description = "Email address to use as sender", required = false) String fromEmail) {

        Map<String, Object> config = new HashMap<>();
        if (host != null) config.put("host", host);
        if (port != null) config.put("port", port);
        if (username != null) config.put("username", username);
        if (password != null) config.put("password", password);
        if (fromEmail != null) config.put("fromEmail", fromEmail);

        boolean success = mcpEmailService.configure(config);

        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "Email service configured successfully" : "Failed to configure email service");

        return response;
    }
}
